//
//  SldeSlipMenu.m
//  侧滑菜单实践1
//
//  Created by administrator on 16/8/9.
//  Copyright © 2016年 duanzhenglong. All rights reserved.
//

#import "SldeSlipMenu.h"

@interface SldeSlipMenu ()

@end

@implementation SldeSlipMenu

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //默认写法
    }
    return self;
}
//初始化方法
-(instancetype)initWithView:(UIViewController *)LeftView andMainView:(UIViewController *)MainView{
    if (self) {
        _speedf=0.5;
        LeftControl=LeftView;
        MainControl=MainView;
        //添加滑动手势
        UIPanGestureRecognizer*pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
        [MainControl.view addGestureRecognizer:pan];
        //添加单击手势
        _sideslipTapGes=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];
        [_sideslipTapGes setNumberOfTapsRequired:1];
        [MainControl.view addGestureRecognizer:_sideslipTapGes];
        //初始隐藏Left视图
        LeftControl.view.hidden=YES;
        //添加两个视图
        [self.view addSubview:LeftControl.view];
        [self.view addSubview:MainControl.view];
        
    }
    return self;
}
#pragma mark-滑动手势实现
-(void)handlePan:(UIPanGestureRecognizer*)rec{
    //返回一个明确的当前位置point
    CGPoint point=[rec translationInView:self.view];//在父类视图上
    //设置被拖动视图的边角为圆形
    rec.view.layer.cornerRadius=10;
    rec.view.layer.masksToBounds=YES;
    //全局偏移量scalef
    scalef=(point.x*self.speedf+scalef);//因为是左右滑动，只涉及到x
    //rec.view这是添加手势的那个视图，这里是MainControl
    if (rec.view.frame.origin.x>=0) {
        rec.view.center=CGPointMake(rec.view.center.x+point.x*self.speedf, rec.view.center.y);//添加手势的视图右移
        //被滑动的视图减小
        rec.view.transform=CGAffineTransformScale(CGAffineTransformIdentity, 1-scalef/1000,1-scalef/1000);
        //因为拖动起来一直是在递增，所以每次都要用setTranslation:方法将每次触摸都设置为0位置，这样才不至于不受控制般滑动出视图
        [rec setTranslation:CGPointMake(0, 0) inView:self.view];
        //左视图显示
        LeftControl.view.hidden=NO;
    }
    //手势结束后修正位置
    if (rec.state==UIGestureRecognizerStateEnded) {
        if (scalef>140*self.speedf) {
            [self showLeftView];
        }
        else{
            [self showMainView];
            //此时无偏移
            scalef=0;
        }
    }
}
#pragma mark-单击手势实现
-(void)handeTap:(UITapGestureRecognizer*)tap{
    if (tap.state==UIGestureRecognizerStateEnded) {
        [UIView beginAnimations:nil context:nil];
        //被点击的视图恢复大小，这里指MainControl
        tap.view.transform=CGAffineTransformScale( CGAffineTransformIdentity, 1.0, 1.0);
        tap.view.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2 );
        [UIView commitAnimations];
        scalef=0;
    }
}

#pragma mark-修改视图位置
//恢复位置
-(void)showMainView{
    [UIView beginAnimations:nil context:nil];
    MainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
    MainControl.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
    ////恢复边角形状
    MainControl.view.layer.cornerRadius=0;
}

//显示左视图
-(void)showLeftView{
    [UIView beginAnimations:nil context:nil];
    MainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
    MainControl.view.center = CGPointMake(340,[UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
}

//状态栏的显示
- (BOOL)prefersStatusBarHidden
{
    return NO; //返回NO表示要显示，返回YES将hiden
}


@end
